package com.example.renyang.happysell.MyUntil;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.renyang.happysell.R;
import com.example.renyang.happysell.Tools.BytetoInt;
import com.example.renyang.happysell.Tools.msell_food;

import org.json.JSONArray;

import infClass.Food;
import infClass.OrderInform;
import infClass.Util;




public class food_detail extends AppCompatActivity {
    static Food foodstt;
    EditText num;//购买份数
    Button buybut;//购买按钮
    Button addbt,subbt;//增减按钮
    private Handler handler;
   // final JSONArray[] obj = {new JSONArray()};

    public static OrderInform orderinform=new OrderInform();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_food_detail);
        new Thread(){
            public void run(){
                foodstt=init();
            }
        }.start();

            handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    switch (msg.what) {
                        case 1:
                            Log.i("zx",foodstt.getFname()+"*************************2");
                            add(foodstt);
                            break;
                        default:
                            break;
                    }

                }
            };
        num=(EditText)findViewById(R.id.order_number_text);
        buybut=(Button)findViewById(R.id.tj_order_bt);
        addbt=(Button)findViewById(R.id.order_number_add);
        subbt=(Button)findViewById(R.id.order_number_sub);
        addbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n=num.getText().toString();
                num.setText(String.valueOf(Integer.parseInt(n)+1));
            }
        });
        subbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n = num.getText().toString();
                int nn = Integer.parseInt(n) - 1;
                num.setText(String.valueOf(nn < 1 ? 1 : nn));
            }
        });
        buybut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String count = num.getText().toString();
                if (count == null || count.trim().length() < 1) return;
                addcar(foodstt, Integer.parseInt(count));
               finish();
            }
        });

    }
    private void add(Food food2) {
        try {
            //      ImageView iv = (ImageView) findViewById(R.id.pic_det);
            //    iv.setImageResource(BytetoInt.bytesToInt(food.getFoodpic(), 0));
       //     TextView des = (TextView) findViewById(R.id.fd_target_des);
         //   des.setText(foodstt.getIntro());
            Log.i("zx", foodstt.getFname() + "*************************3");
            Food food1=new Food();
            Log.i("zx",food2.getFname()+"*************************4");
            food1=food2;
            TextView name = (TextView) findViewById(R.id.fd_name_title);
            name.setText(food1.getFname());
            TextView price = (TextView) findViewById(R.id.fd_target_des);
            price.setText(food1.getPrice());
            TextView storename = (TextView) findViewById(R.id.fd_shop_name);
            storename.setText(food1.getStorename());
            Log.i("zx","*************************5");
        }catch(Exception e){
            e.printStackTrace();
            Log.i("zx","init(food) 时候，得到值设置时候出错");
        }

    }

    /**
     * 产生订单对象
     * @param food
     * @param  acount 数量
     */
    protected void addcar(Food food,int acount){
        Log.i("zx", "加入购物车");
        Log.i("zx",food.getFname()+"get+++++++++++++++++++++++");
        Car.addcarmap(food.getFname(), String.valueOf(acount));
        Log.i("zx",Car.getCarmap().toString()+"enter car+++++++++++++++++++++1+++++++++++++++++++++++++1");

        /*
        Util u = new Util();
        String s = "username"+u.getTime();
        OrderInform order=new OrderInform();
        order.setId(s);
        */
    }
    //初始化食物对象
    private Food init(){
        Food food=null;
        try {
             food=new Food();
            Bundle bundle = this.getIntent().getExtras();
            food.setFname(bundle.getString("name"));
            food.setStorename(bundle.getString("storename"));
            food.setIntro(bundle.getString("info"));
            food.setPrice(bundle.getString("price"));
            Log.i("zx",food.getFname()+"*******************0");
            Message msg=new Message();
            msg.what=1;
            handler.sendMessage(msg);
            Log.i("zx", "*************************1");
            return food;
        }catch(Exception e){
            e.printStackTrace();
            Log.i("zx","得到数据时候报错");
        }
        return food;

    }
}
